package com.hzit.sk.service.impl;

import com.hzit.sk.enums.OrderStatusEnum;
import com.hzit.sk.mapper.OrderInfoMapper;
import com.hzit.sk.model.OrderInfo;
import com.hzit.sk.mq.CreateOrderReq;
import com.hzit.sk.resp.Response;
import com.hzit.sk.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 订单业务接口实现类
 */
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Override
    public void createOreder(CreateOrderReq createOrderReq) {

        OrderInfo record = new OrderInfo();
        record.setUserId(Long.parseLong(createOrderReq.getUserId()));
        record.setGoodsId(Long.parseLong(createOrderReq.getGoodsId()));
        record.setStatus(OrderStatusEnum.UNPAY.getCode());
        record.setCreateDate(new Date());
        orderInfoMapper.insertSelective(record);


    }

    @Override
    public Response<String> queryOreder(String userId, String goodsId) {

        Response<String> response = Response.getSuccessData("请求成功");



        List<OrderInfo> list = orderInfoMapper.queryByGoodsAndUserId(goodsId,userId);
        if(CollectionUtils.isEmpty(list)){
            response.setData("0");
            return response;
        }

        if(list.get(0).getStatus() == -1){
            response.setData("-1");
            return response;
        }
        response.setData("1");

        return response;
    }
}
